// pages/test/test.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js')
var demo = new QQMapWX({
  key: 'OMYBZ-RE4HF-H6NJD-NM3DP-I7DQH-XHFLG' // 申请的key
})
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchAd: [],
    myAd: [],
    aroundAd: []
  },
  onLoad: function () {
    // console.log(app.globalData)
    //获取周围10家写字楼 即附近地址
    let that = this
    that.setData({
      local: app.globalData.nowLocal.localName
    })
    demo.search({
      location: {
        latitude: app.globalData.nowLocal.local.lati,
        longitude: app.globalData.nowLocal.local.longi
      },
      page_size: 10,
      keyword: '写字楼',
      success: function (res) {
        // console.log(res)
        that.setData({
          aroundAd:res.data
        })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  fuzzySearch: function (e) {
    let that = this
    demo.getSuggestion({
      region: app.globalData.city,
      keyword: e.detail,
      region_fix:1,
      policy: 1,
      success: function (res) {
        that.setData({
          searchAd:res.data
        })
      },
      fail: function (res) {
        //在此隐藏搜索地址模块
        // console.log(res);
        that.setData({
          searchAd: []
        })
      }
    })
  },
  /**
   * 点击地址获取地址经纬度
   */
  chooseAd: function(e){
    let localName = e.target.dataset.address.title
    let local = e.target.dataset.address.location
    app.globalData.chooseLocal.localName = localName
    app.globalData.chooseLocal.local = local
    app.globalData.isChooseAd = true
    console.log(e.target.dataset.address)
    wx.navigateBack()
  },
  /**
   * 返回当前定位地址
   */
  backToLocal: function(){
    app.globalData.isChooseAd = false
    wx.navigateBack()
  },
  /**
   * 清空搜索
   */
  cancleSearch: function(){
    this.setData({
      searchAd: []
    })
    console.log(!this.data.searchAd)
  }
})